<?php
namespace app\track\service;
use app\track\model\TimeTrackModel as pa;
use app\track\validata\TimeTrackValidata as tv;
use app\base\service\BaseService;
use think\facade\Log;
class TimeTrackService extends BaseService 
{
	protected $track ='';
	protected $tv='';
	public function __construct()
	 {
	 	parent::__construct();
	 	$this->track = new pa;
	 	$this->tv = new tv;
	 }
     //时光轨迹时间列表
     protected function time_lists($data){
          $res = $this->track->time_lists($data);
          if(!empty($res))
            return $this->return_code(200,"成功",$res);
        return $this->return_code(202,"数据为空");
       
     }
     //时光轨迹列表
      protected function track_lists($data){
        //查出任务、相册、蜗牛圈的动态
             if (!$this->tv->scene('track_lists')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
        $res = $this->track->track_lists($data);
        if(!empty($res))
            return $this->return_code(200,"成功",$res);
        return $this->return_code(202,"数据为空",$res);
      }
	 //发布时光轨迹
	  protected function release_track($data){
	  	$data['image']=[];

	  	if(!is_array($data['imagefile']))  return $this->return_code(405,'文件不是数组形式');
		// 描述 图片
		if (!$this->tv->scene('release_track')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
     //判断权限 家人和自己可以添加
        $perm = $this->permissions($data['Openid']);
        if($perm['code'] !=10000||$perm['code']!=10001)   return $this->return_code(301,"对不起，权限不足");
        //通过Openid获得用户信息
        if(isset($data['Openid'])&&!empty($data['Openid'])){
            $userinfo = $this->get_userinfo($data['Openid']);
        }else{
            $userinfo = $this->user;
        }
        $data['Token'] = $userinfo['Token'];
        $data['Openid'] = $this->user['Openid'];
        $data['Userid'] = $this->user['Userid'];
        //上传图片
        $imageres=$this->upload_img($data['imagefile']);
        //log::write($imageres);
        if($imageres['code']==200){
        	//操作数据库
        	foreach ($imageres['imagepath'] as $key => $value) {
        		$data['image'][]=[
        			'picsname'=>(isset($data['picsname'][$key])&&!empty($data['picsname'][$key]))?$data['picsname'][$key]:'',
        			'Picspath'=>$value,
        			'Openid'=>$this->user['Openid'],
					'createtime'=>time(),
					'Userid'=>$this->user['Userid'],
					'trackid'=>'1',
					'ordernum'=>0
        		];
        	}

        	if($this->track->publish($data)) return $this->return_code(200,'成功');

        }else{
        	return $this->return_code(300,$imageres['msg']);
        }

        return $this->return_code();
	  }

	  //查看时光轨迹列表
	  // public function track_list($data){
	  // 	if (!$this->tv->scene('track_list')->check($data)) {
   //          return $this->return_code(300,$this->tv->getError());
   //      }
   //        //判断权限
   //      $perm = $this->permissions($data['Openid']);
   //      //通过Openid获得用户信息
   //      if(isset($data['Openid'])&&!empty($data['Openid'])){
   //          $userinfo = $this->get_userinfo($data['Openid']);
   //      }else{
   //          $userinfo = $this->user;
   //      }
   //        switch ($perm['code']) {
   //          case '10000':
                
   //              break;
   //          case '10001'://家人
   //               $where['t.permissions']   = array('neq' => 3);
   //              break;
   //          case '10002'://好友
   //              $where['t.permissions']   = array('neq' => 3);
   //          break;
            
   //          default:
   //              $where['t.permissions']   = 1;
   //              break;
   //      }

   //      $where['t.Token'] = $userinfo['Token'];
   //      $res = $this->track->track_list($data,$where,$data['page'],$data['limit']);
   //      return $this->return_code(200,"查询成功",$res);
	  // }


    //删除时光轨迹
      public function del_track($data){
        if (!$this->tv->scene('del_track')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
        $where_p['trackid'] = $data['trackid'];
        $where_p['Openid'] = $this->user['Openid'];
       $photo = $this->track->findAllByWhere('time_track_pics',$where_p,'picsid,Picspath','picsid desc');
       if($is_delete){
            //循环删除本地图片
            foreach ($photo as $k => $v) {
                 $res_image_code =$this->del_file($v['Picspath']);
            }
       }
        //删除数据库相关信息
         $res = $this->track->del_track($where_p,$data['is_delete']);
         if($res) return $this->return_code(200,'相册删除成功');
         return $this->return_code();
      }
	  //查看评论
	  public function comment_list($data){
	  	if (!$this->tv->scene('comment_list')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }

        $res = $this->track->comment_list($data,$data['page'],$data['limit']);
        return $this->return_code(200,'查看成功',$res);
	  }

	  //添加评论
	  	protected function comments($data){
		if (!$this->tv->scene('comment')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
        $comment=[
        	'content'=>$data['content'],
        	'Openid' =>$this->user['Openid'],
        	'createtime'=>time(),
        	'Userid' => $this->user['Userid'],
        	'ordernum' =>0,
        	'Hits'=>0,
        	'number'=>0,
        	'trackid'=>$data['trackid'],
        	'Token'=>$this->user['Token'],
        	'Replayid'=>$data['Replayid']
        ];
         $id = $this->track->comment($comment);
         if($id){
        	 return $this->return_code(200,'评论成功');
        }
        return  $this->return_code();

	}

          //点赞和取消
        protected function add_points($data){
        if (!$this->tv->scene('add_points')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
        $where['Openid'] = $this->user['Openid'];
        $where['trackid'] = $data['trackid'];
//$name,$where = array(),$fileds="*"
        $point = $this->track->findByAttributes('track_point',$where,'status');
     if(empty($point)){
        $point=[
            'Openid' =>$this->user['Openid'],
            'createtime'=>time(),
            'Userid' => $this->user['Userid'],
            'status' =>1,
            'trackid'=>$data['trackid'],
        ];
         $id = $this->track->add_points($point);
         if($id){
             return $this->return_code(200,'点赞成功');
        }
     }else{
        if($point['status'] == 1){
            $up['status']=2; 
        }else{
            $up['status']=1; 
        }
          $up_p = $this->track->updateByWhere('track_point',$where,$up);
          if($up_p)   return $this->return_code(200,'成功');
     }
        
        
        return  $this->return_code();

    }

 

}